<?php

class Admin_Model_Roles extends Zend_Db_Table_Abstract
{
    protected $_name = 'roles';
    protected $_primary = 'id';
    protected  $_order = 'roleName';
    
    public function getAllData ()
    {
		$query = $this->select();
   		return $this->fetchAll($query);
    }
    public function addData($roleName)
    {
        $data = array('roleName' => $roleName);
        return $this->insert($data);
    }
    public function editData($roleId, $roleName)
    {
        $data = array('roleName' => $roleName);
        return $this->update($data, 'id = '.$roleId);
    }
    public function getDataByName($roleName)
    {
        $query = $this->select()
                        ->from(array('r'=>'roles'), array('r.roleName', 'r.id'))
                        ->where('r.roleName = ?',$roleName);
                        
    	return $this->fetchRow($query);
    }
    public function getDataByRoleId($roleid)
    {  
        $query = $this->select()
                        ->from(array('r'=>'roles'), array('r.roleName', 'r.id'))
                        ->where('r.id = ?',$roleid);
                                        
    	return $this->fetchRow($query);
    }
    public function getRolesUser($userId)
    {
        $query = $this->select();
        $query->setIntegrityCheck(false);
        $query->from(array('r'=>'roles'), array('r.roleName', 'r.id'));
        $query->join(array('ur'=>'users_roles'),'r.id = ur.roleId');
        $query->where('ur.userId = ?',$userId);
                                        
    	return $this->fetchAll($query);
    }
    public function deleteDataByRoleId($id)
    {
        $this->delete("id=".(int)$id);
    }
}

























